<template>
    <div>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="10">
                    <!-- 搜索添加 -->
                    <el-input
                            placeholder="请输入搜索内容"
                            v-model="queryInfo.queryString"
                            clearable
                            @clear="findPage"
                    >
                        <el-button slot="append" icon="el-icon-search" @click="findPage" />
                    </el-input>
                </el-col>
                <el-col :span="2">
                    <el-button
                            @click="insert"
                            style="margin-left: 10px"
                            type="primary"
                    >新增食物方案</el-button
                    >
                </el-col>
            </el-row>
            <el-table
                    :data="tableList"
                    stripe
                    border
                    highlight-current-row
                    v-loading="loading"
                    element-loading-text="拼命加载中"
                    element-loading-spinner="el-icon-loading"
                    max-height="450"
            >
                <el-table-column type="index" label="序号" width="80">
                </el-table-column>
                <el-table-column prop="instructions" label="使用方案" width="180">
                </el-table-column>
                <el-table-column prop="suggestion" label="使用建议">
                </el-table-column>
                <el-table-column prop="benefit" label="方案效益"> </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template slot-scope="scope">
                        <!-- 修改 -->
                        <el-button
                                type="primary"
                                icon="el-icon-edit"
                                size="mini"
                                @click="updateById(scope.row)"
                        />
                        <el-button
                                type="danger"
                                icon="el-icon-delete"
                                size="mini"
                                @click="deleteById(scope.row.id)"
                        />
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="查看详情" width="80">
                    <template slot-scope="scope">
                        <el-button
                                @click="openXq(scope.row)"
                                type="success"
                                icon="el-icon-s-check"
                                size="small"
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block">
                <el-pagination
                        @size-change="handlePageSize"
                        @current-change="handlePageNumber"
                        background
                        :current-page="queryInfo.pageNumber"
                        :page-sizes="[8, 16, 32, 64, 128]"
                        :page-size="queryInfo.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                >
                </el-pagination>
            </div>
        </el-card>

        <!-- 表单添加/修改 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                @close="dialogClose"
                width="70%"
        >
            <el-tabs
                    type="border-card"
                    v-model="activeName"
                    @tab-click="handleClick"
            >
                <el-tab-pane label="食物方案" name="first">
                    <el-form v-if="xq === false" :model="form" :rules="rules" ref="form" label-width="100px">
                        <el-row>
                            <el-col>
                                <el-col :span="4">
                                    <el-form-item label="健康计划">
                                        <el-select style="width: 150px" v-model="form.id" placeholder="请选择健康计划">
                                            <el-option
                                                    v-for="(item, index) in tPlanList"
                                                    :key="index"
                                                    :label="item.title"
                                                    :value="item.id"
                                                    @click.native="findFoodByTypeId(item)"
                                            >
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-col>
                        </el-row>
                        <el-divider> 方案主体 </el-divider>
                        <el-card>
                            <el-row>
                                <el-col :span="18">
                                    <el-form-item label="使用说明" prop="instructions">
                                        <el-input
                                                v-model="form.instructions"
                                                type="textarea"
                                                placeholder="请输入使用说明"
                                                show-word-limit
                                                maxlength="50"
                                                :autosize="{ minRows: 1, maxRows: 2 }"
                                        />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="18">
                                    <el-form-item label="使用建议" prop="suggestion">
                                        <el-input
                                                v-model="form.suggestion"
                                                type="textarea"
                                                placeholder="请输入使用建议"
                                                show-word-limit
                                                maxlength="300"
                                                :autosize="{ minRows: 2, maxRows: 5 }"
                                        />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="18">
                                    <el-form-item label="方案效益" prop="benefit">
                                        <el-input
                                                v-model="form.benefit"
                                                type="textarea"
                                                placeholder="请输入方案效益"
                                                show-word-limit
                                                maxlength="800"
                                                :autosize="{ minRows: 5, maxRows: 30 }"
                                        />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-card>
                        <el-divider> 方案食物 </el-divider>
                        <el-card v-if="isHaveFood === true" shadow="hover" class="permissions-class">
                            <el-row :gutter="20">
                                <el-col :span="8">
                                    <el-form-item label="是否添加食物" v-if="isHaveFood === true">
                                        <el-checkbox
                                                v-model="isHaveFood"
                                        />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="4">
                                    <el-form-item label="食物分类" v-if="isHaveFood === true">
                                        <el-select style="width: 150px" v-model="form.foodType" placeholder="请选择食物分类">
                                            <el-option
                                                    v-for="(item, index) in checkFoodTypeList"
                                                    :key="index"
                                                    :label="item.title"
                                                    :value="item.id"
                                                    @click.native="findFoodByTypeId(item)"
                                            >
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="isHaveFood === true" :span="24">
                                    <div slot="header">
                                        <span>食物集合</span>
                                    </div>
                                    <el-checkbox-group v-model="checkedFoodList">
                                        <el-checkbox
                                                v-for="(item, index) in foodList"
                                                border
                                                class="permissions-class1"
                                                size="small"
                                                :label="item.title"
                                                :key="index"
                                                @change="foodChange(item)"
                                        />
                                    </el-checkbox-group>
                                </el-col>
                            </el-row>
                        </el-card>
                    </el-form>

                    <el-form v-if="xq === true" :model="xqForm" :rules="rules" ref="xqForm">
                        <el-row :gutter="20">
                            <el-col :span="8">
                                <el-form-item label="评估单号" prop="orderId">
                                    <el-select disabled v-model="xqForm.orderId" placeholder="请选择评估单号">
                                        <el-option
                                                v-for="(item, index) in checkOrderList"
                                                :key="index"
                                                :label="item.orderId"
                                                :value="item.id"
                                                @click.native="findMerByOrderId(item)"
                                        >
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="健康员" prop="assessor">
                                    <el-select disabled v-model="xqForm.assessor" placeholder="请选择健康员">
                                        <el-option
                                                v-for="(item, index) in assessorList"
                                                :key="index"
                                                :label="item.nickName"
                                                :value="item.nickName"
                                        >
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="计划执行人" prop="planUser">
                                    <el-input v-model="xqForm.planUser" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="8">
                                <el-form-item label="开始日期" prop="evDate">
                                    <template>
                                        <el-date-picker disabled
                                                        v-model="xqForm.planUp"
                                                        align="right"
                                                        type="date"
                                                        format="yyyy 年 MM 月 dd 日"
                                                        value-format="yyyy-MM-dd"
                                        >
                                        </el-date-picker>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="结束日期" prop="evDate">
                                    <template>
                                        <el-date-picker disabled
                                                        v-model="xqForm.planEnd"
                                                        align="right"
                                                        type="date"
                                                        format="yyyy 年 MM 月 dd 日"
                                                        value-format="yyyy-MM-dd"
                                        >
                                        </el-date-picker>
                                    </template>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="计划标题" prop="title">
                                    <el-input
                                            disabled
                                            v-model="xqForm.title"
                                            type="textarea"
                                            placeholder="请输入健康计划标题"
                                            show-word-limit
                                            maxlength="50"
                                            :autosize="{ minRows: 1, maxRows: 2 }"
                                    />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="计划目标" prop="planGoal">
                                    <el-input
                                            disabled
                                            v-model="xqForm.planGoal"
                                            type="textarea"
                                            placeholder="请输入计划目标"
                                            show-word-limit
                                            maxlength="300"
                                            :autosize="{ minRows: 2, maxRows: 5 }"
                                    />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="方案说明" prop="remark">
                                    <el-input
                                            disabled
                                            v-model="xqForm.remark"
                                            type="textarea"
                                            placeholder="请输入评估说明"
                                            show-word-limit
                                            maxlength="800"
                                            :autosize="{ minRows: 5, maxRows: 30 }"
                                    />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane v-if="xq === true" label="食物方案" name="fore">
                    <el-table
                            :data="planFoodList"
                            stripe
                            border
                            highlight-current-row
                            max-height="450"
                    >
                        <el-table-column type="index" label="序号" width="80">
                        </el-table-column>
                        <el-table-column prop="instructions" label="使用方案" width="180">
                        </el-table-column>
                        <el-table-column prop="suggestion" label="使用建议">
                        </el-table-column>
                        <el-table-column prop="benefit" label="方案效益"> </el-table-column>
                    </el-table>

                    <el-divider></el-divider>
                    <span>方案食物列表</span>
                    <el-table
                            :data="foodList"
                            stripe
                            border
                            highlight-current-row
                            max-height="450"
                    >
                        <el-table-column type="index" label="序号" />
                        <el-table-column label="食物名称" prop="title" />
                        <el-table-column label="食物类别" prop="typeId" />
                        <el-table-column label="缩略图" align="center" width="130">
                            <template slot-scope="scope">
                                <el-image
                                        v-if="scope.row.imageUrls"
                                        style="width: 100px; height: 100px"
                                        :src="$qiniu + scope.row.imageUrls.split(',')[0]"
                                        :preview-src-list="previewImages(scope.row.imageUrls.split(','))"
                                />
                                <el-image
                                        v-if="!scope.row.imageUrls"
                                        style="width: 100px; height: 100px"
                                        src=""
                                />
                            </template>
                        </el-table-column>
                        <el-table-column label="营养元素" prop="nutrient" />
                        <el-table-column label="热量" prop="heat" />
                        <el-table-column label="蛋白质" prop="protein" />
                        <el-table-column label="脂肪" prop="fat" />
                        <el-table-column label="碳水化合物" prop="carbonWater" />
                        <el-table-column label="膳食纤维" prop="dietaryFiber" />
                        <el-table-column label="维生素A" prop="vitaminA" />
                        <el-table-column label="维生素C" prop="vitaminC" />
                        <el-table-column label="维生素E" prop="vitaminE" />
                        <el-table-column label="胡萝卜素" prop="carrot" />
                        <el-table-column label="维生素B1" prop="vitaminB1" />
                        <el-table-column label="维生素B2" prop="vitaminB2" />
                        <el-table-column label="烟酸" prop="niacin" />
                        <el-table-column label="胆固醇" prop="cholesterol" />
                        <el-table-column label="镁" prop="magnesium" />
                        <el-table-column label="铁" prop="iron" />
                        <el-table-column label="钙" prop="calcium" />
                        <el-table-column label="锌" prop="zinc" />
                        <el-table-column label="铜" prop="copper" />
                        <el-table-column label="锰" prop="manganese" />
                        <el-table-column label="钾" prop="potassium" />
                        <el-table-column label="磷" prop="phosphorus" />
                        <el-table-column label="钠" prop="sodium" />
                        <el-table-column label="硒" prop="selenium" />
                    </el-table>
                </el-tab-pane>
            </el-tabs>
            <div slot="footer" class="dialog-footer">
                <el-button @click="clickCancel">取 消</el-button>
                <el-button type="primary" @click="clickOk">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                // 分页查询条件
                queryInfo: {
                    // 页码
                    pageNumber: 1,
                    // 页数
                    pageSize: 8,
                    // 关键字
                    queryString: null,
                },
                // 表格数据
                tableList: [],
                // 总记录数
                total: 0,
                // 表单标题
                title: null,
                // 是否打开对话框
                open: false,
                // 表格加载
                loading: false,
                // 计划表单数据
                form: {},
                xqForm: {},
                // 食物分类
                checkFoodTypeList: [],
                // 方案食物食物
                foodList: [],
                // 选定的集合
                checkedFoodList: [],
                // 未分配食物的健康计划
                tPlanList: [],
                // 提交的
                upFoods: [],
                xq: null,
                activeName: "first",
                rules:{},
                isHaveFood: true,
            }
        },
        created() {
            this.findPage();
        },
        methods:{
            /** 分页查询 */
            findPage() {
                this.loading = true;
                this.$ajax.post("/planFood/findPlanFoodPage", this.queryInfo).then((res) => {
                    console.log("查询数据",res);
                    this.loading = false;
                    this.tableList = res.data.data.rows;
                    this.total = res.data.data.total;
                });
            },
            /** 页面大小改变事件 */
            handlePageSize(newPageSize) {
                this.queryInfo.pageSize = newPageSize;
                this.findPage();
            },
            /** 页面改变事件 */
            handlePageNumber(newPageNumber) {
                // 将分页参数新数据传递给pageNumber(分页参数)
                this.queryInfo.pageNumber = newPageNumber;
                this.findPage();
            },
            insert(){
                this.open = true;
                this.findAllType();
                this.findFoodStatus();
                this.xq = false;
            },
            openXq(){

            },
            updateById(row){
                console.log("row", row);
                this.checkedFoodList = [];
                this.title = "修改食物方案";
                this.form = row;
                //  遍历食物集合 回显
                row.foods.forEach((item) => {
                    this.checkedFoodList.push(item.title);
                    this.upFoods.push(item);
                });
                this.findAllType();
                this.open = true;
                this.xq = false;
                this.isHaveFood = true;
            },
            /** 关闭对话框事件 */
            dialogClose() {
                // 将表单进行重置，并移除校验效果
                this.$refs.form.resetFields();
                this.$refs.xqForm.resetFields();
                this.xq = null;
                this.isHaveFood = true;
            },
            /** 获取所有食物分类 */
            findAllType(){
                this.$ajax.get("/food/typeAll").then((res) => {
                    console.log("查询食物分类",res);
                    this.checkFoodTypeList = res.data.data;
                });
            },
            /** 根据食物分类查询食物信息 */
            findFoodByTypeId(item) {
                console.log("item",item);
                this.$ajax.post(`/food/findByTypeId/${item.id}`).then((res) => {
                    console.log("获取分类食物",res);
                    this.foodList = res.data.data;
                });
            },
            /** 查询所有未分配食物的健康计划 */
            findFoodStatus(){
                this.$ajax.get("/tPlan/findFoodStatus/").then((res) => {
                    console.log("获取健康计划",res);
                    this.tPlanList = res.data.data;
                });
            },
            /** 点击取消 */
            clickCancel() {
                // 将表单进行重置，并移除校验效果
                this.form = {};
                this.open = false;
                this.xq = null;
                this.isHaveFood = true;
                this.findPage();
                this.editSelectStatus = null;
                this.planUserStatus = false;
            },
            /** 点击确定 */
            clickOk() {
                // 进行表单校验
                this.$refs.form.validate((valid) => {
                    // 校验不通过
                    if (!valid) {
                        return this.$message.error("表单校验失败,请检查后提交！");
                    }
                    // 校验通过
                    const a = [];
                    this.a = this.form.value;
                    if(a > 0){
                        this.form.planUp  = this.form.value[0];
                        this.form.planEnd = this.form.value[1];
                    }
                    this.form.foods = this.upFoods;
                    if (this.form.id === undefined || this.form.id === null) {
                        // this.$message.success("提交");
                        // this.$ajax.post("/bodyEvaluate/insert/", this.form).then((res) => {
                        //     this.$message.success(res.data.message);
                        //     this.open = false;
                        //     this.findPage();
                        // });
                        console.log("this.form",this.form)
                    } else {
                        this.$ajax.put("/bodyEvaluate/update/", this.form).then((res) => {
                            this.$message.success(res.data.message);
                            this.open = false;
                            this.findPage();
                        });
                    }
                });
            },
            handleClick(tab, event){

            },
            /** 勾选权限的事件 */
            foodChange(item) {
                // 点击为勾选状态就添加到upFoods[]
                console.log("传入的食物", item);
                if (event.target.checked) {
                    this.upFoods.push(item);
                } else {
                    for (let i = 0; i < this.upFoods.length; i++) {
                        if (this.upFoods[i].id === item.id) {
                            this.upFoods.splice(i, 1);
                        }
                    }
                }
                console.log("勾选的食物", this.upFoods);
            },
            //预览图片列表
            previewImages(list) {
                let arr = [];
                list.forEach((item) => {
                    arr.push(this.$qiniu + item);
                });
                return arr;
            },
        }
    }
</script>

<style scoped>
    .status-class {
        margin-left: -30%;
    }
    .permissions-class {
        text-align: left;
    }
    .permissions-class1 {
        margin: 5px;
    }
</style>
